package mainPack;

import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Scanner;

import javax.activity.ActivityRequiredException;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;

public class Algorithm {

	private Scanner inputScanner;

	private List <Battery> Batteries;

	private int SystemVoltage;
	private int NumberP;
	private int MinMW;
	private int BillWh;
	private int AppWh;
	private int ACWh;
	private double DoD;
	private int BillSummer;
	private int MaxEnergyPerMonth;
	private double MaxPercentage;

	private List <UsedBattery> UsedBatteries;
	private List <Appliance> WashingMachines;
	private List <Appliance> Ovens;
	private List <Appliance> Freezers;
	private List <Appliance> Refrigerators;
	private List <Appliance> AirConditioners;
	private List <Appliance> WaterHeaters;
	private List <Appliance> Microwaves;
	private List <Appliance> Dishwashers;

	public Algorithm(){
		inputScanner = new Scanner(System.in);
		UsedBatteries=new ArrayList<UsedBattery>();
		WashingMachines=new ArrayList<Appliance>();
		Ovens=new ArrayList<Appliance>();
		Dishwashers=new ArrayList<Appliance>();
		AirConditioners=new ArrayList<Appliance>();
		Freezers=new ArrayList<Appliance>();
		Refrigerators=new ArrayList<Appliance>();
		Microwaves=new ArrayList<Appliance>();
		WaterHeaters=new ArrayList<Appliance>();
		Batteries=new ArrayList<Battery>();
		BillWh=0;
		AppWh=0;
		DoD=1.3; //Depth of Discharge = 30%
		parser();
	}

	public void Algo(){


		//CALCULATE BATTERIES DEPENDING ON INFO
		MaxPercentage=(double)MaxEnergyPerMonth/(double)BillWh;
		if (MaxPercentage>0.01){
		if(!WashingMachines.isEmpty())
		WashingMachineCalculator();
		if(!Refrigerators.isEmpty()||!Freezers.isEmpty())
		RefrigeratorFreezerCalculator();
		if(!AirConditioners.isEmpty())
		AirConditionerCalculator();
		if(!WaterHeaters.isEmpty())
		WaterHeaterCalculator();
		if(!Microwaves.isEmpty()||!Ovens.isEmpty()||!Dishwashers.isEmpty())
		KitchenCalculator();
		ExtraCalculator();

		//SHOW RESULTS
		PrintBatteriesList(UsedBatteries);
		System.out.println();
		}
		else{
			System.out.println("Too few energy coverage required");	
		}
	}

	private void parser() {	

		try{
			FileReader reader = new FileReader("Input/InputData.json");
			JSONParser jsonParser = new JSONParser();
			JSONObject jsonObject = (JSONObject) jsonParser.parse(reader);
			
			NumberP=(int)(long)jsonObject.get("people");
			SystemVoltage=(int)(long)jsonObject.get("systemvoltage");
			MinMW=(int)(long)jsonObject.get("MWminutes");
			BillWh=(int)(long)jsonObject.get("billWh");
			MaxEnergyPerMonth=(int)(long)jsonObject.get("maxWhcovered");
			BillSummer=(int)(long)jsonObject.get("summer");

			JSONArray washingmachines = (JSONArray) jsonObject.get("washingmachines");	
			Iterator i = washingmachines.iterator(); 
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				WashingMachines.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
				//WashingMachines.add(new Appliance("WashingMachine",2200,900,0));
			}

			JSONArray ovens = (JSONArray) jsonObject.get("ovens");
			i = ovens.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				Ovens.add(new Appliance("Oven",1600,1200,0));						//per cycle
				//				Ovens.add(new Appliance("LittleOven",600,800,0));                    //Little
				Ovens.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray dishwashers = (JSONArray) jsonObject.get("dishwashers");
			i = dishwashers.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				Dishwashers.add(new Appliance("DishWasher",2200,1200,0));
				Dishwashers.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray refrigerators = (JSONArray) jsonObject.get("refrigerators");
			i = refrigerators.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				Refrigerators.add(new Appliance("Refrigerator",140,1100,0));
				Refrigerators.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray freezers = (JSONArray) jsonObject.get("freezers");
			i = freezers.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				Freezers.add(new Appliance("Freezer",150,1100,0));
				Freezers.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray air_conditioners = (JSONArray) jsonObject.get("air-conditioners");
			i = air_conditioners.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				AirConditioners.add(new Appliance("AirConditioner",1700,8000,1));
				AirConditioners.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray waterheaters = (JSONArray) jsonObject.get("waterheaters");
			i = waterheaters.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				WaterHeaters.add(new Appliance("WaterHeater",2700,1300,0));
				WaterHeaters.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));
			}

			JSONArray microwaves = (JSONArray) jsonObject.get("microwaves");
			i = microwaves.iterator();
			while (i.hasNext()) {
				JSONObject innerObj = (JSONObject) i.next();
				//				Microwaves.add(new Appliance("MicroWave",1100,23,0));
				Microwaves.add(new Appliance(String.valueOf(innerObj.get("Name")), (int)(long)innerObj.get("MaxPower"),(int)(long)innerObj.get("AverageConsumption")));

			}
			
			JSONArray batteries = (JSONArray) jsonObject.get("batteries");	
			Iterator j = batteries.iterator(); 
			while (j.hasNext()) {
				JSONObject innerObj = (JSONObject) j.next();
				Batteries.add(new Battery(String.valueOf(innerObj.get("Model")),String.valueOf(innerObj.get("Producer")), (int)(long)innerObj.get("Type"),(int)(long)innerObj.get("Price"), (int)(long)innerObj.get("Weight"), (double)innerObj.get("Ah"), (double)innerObj.get("Wh"), (double)innerObj.get("DischargePower"), (int)(long)innerObj.get("Voltage"), (int)(long)innerObj.get("Cycles")));
			}

		}catch (Exception ex) {
			ex.printStackTrace();
		}
	}

	private void WashingMachineCalculator(){
		int TotWh = 0; //(int)( NumberWM*WashingMachine.getAverageConsumption()*1.3);
		Iterator<Appliance> it = WashingMachines.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TotWh=TotWh+tempa.getAverageConsumption();
		}
		TotWh=(int)(TotWh*DoD*MaxPercentage);
		double peakWh=0;
		int i;
		double temp;
		double minValLit;
		int minLit;
		double minValLa;
		int minLa;
		minValLit=Batteries.get(0).calculateCost(TotWh,SystemVoltage);
		minLit=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage);
				if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit=i;
					minValLit=temp;
				}
			}
		}
		it = WashingMachines.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			peakWh=peakWh+(tempa.getMaxPower()-Batteries.get(minLit).getDischargePower())*0.5;
		}
		peakWh=peakWh*MaxPercentage;
		minValLa=Batteries.get(1).calculateCost((int)peakWh,SystemVoltage);
		minLa=1;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost((int)peakWh,SystemVoltage);
				if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa=i;
					minValLa=temp;
				}
			}
		}
		System.out.printf("%nFor Washing Machines we suggest the following batteries:%n");
		UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Washing Machines", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh-peakWh)/Batteries.get(minLit).getWh()))));
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		UsedBatteries.add(new UsedBattery(Batteries.get(minLa), "Washing Machines", (int)(Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles())*(SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil(peakWh/Batteries.get(minLa).getWh()))));
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		System.out.printf("The total cost of these batteries is %d E%n",(UsedBatteries.get(UsedBatteries.size()-2).getPrice()*UsedBatteries.get(UsedBatteries.size()-2).getQuantity())+(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		System.out.printf("With an average usage of 144 cycles per year their estimated life time is %d years",UsedBatteries.get(UsedBatteries.size()-2).getCycles()/144);
		AppWh=AppWh+(int)((TotWh*12)/DoD);
	}

	private void RefrigeratorFreezerCalculator(){
		//int TotWh = (int)( (NumberR*Refrigerator.getAverageConsumption()+NumberF*Freezer.getAverageConsumption())*1.3);
		int TotWh = 0;
		Iterator<Appliance> it = Refrigerators.iterator();
		while(it.hasNext()){
			Appliance temp = it.next();
			TotWh=TotWh+temp.getAverageConsumption();
		}
		it = Freezers.iterator();
		while(it.hasNext()){
			Appliance temp = it.next();
			TotWh=TotWh+temp.getAverageConsumption();
		}
		TotWh=(int)(TotWh*DoD*MaxPercentage);
		int i;
		double temp;
		double minValLit;
		int minLit;
		double minValLa;
		int minLa;

		minValLit=Batteries.get(0).calculateCost(TotWh,SystemVoltage);
		minLit=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage);
				if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit=i;
					minValLit=temp;
				}
			}
		}

		minValLa=Batteries.get(1).calculateCost(TotWh,SystemVoltage);
		minLa=1;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage);
				if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa=i;
					minValLa=temp;
				}
			}
		}
		minValLit=Batteries.get(minLit).calculateCostNoCycles(TotWh,SystemVoltage);
		minValLa=Batteries.get(minLa).calculateCostNoCycles(TotWh,SystemVoltage);

		System.out.printf("%n%nFor Refrigerator and Freezer we suggest the following batteries:%n");

		if(minValLit<(minValLa*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))) {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Refrigerator and Freezer", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLit).getWh()))));
		}
		else {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLa), "Refrigerator and Freezer", (int)((SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLa).getWh())*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))));
		}
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		System.out.printf("The total cost of these batteries is %dE%n",(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		System.out.printf("With a daily usage their estimated life time is %d years",UsedBatteries.get(UsedBatteries.size()-1).getCycles()/365);
		AppWh=AppWh+(int)((TotWh*30)/DoD);
	}


	private void AirConditionerCalculator(){
		//int TotWh = (int)( (NumberAC*AirConditioner.getAverageConsumption())*1.3);
		int TotWh = 0;
		int maxPower = 0;
		Iterator<Appliance> it = AirConditioners.iterator();
		while(it.hasNext()){
			Appliance temp = it.next();
			TotWh=TotWh+temp.getAverageConsumption();
			if (temp.getMaxPower()>maxPower) maxPower=temp.getMaxPower();
		}
		TotWh=(int)(TotWh*DoD*MaxPercentage);
		int i;
		double temp;
		double minValLit;
		int minLit;
		double minValLa;
		int minLa;

		minValLit=Batteries.get(0).calculateCost(TotWh,SystemVoltage,maxPower);
		minLit=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage,maxPower);
				if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit=i;
					minValLit=temp;
				}
			}
		}

		minValLa=Batteries.get(1).calculateCost(TotWh,SystemVoltage,maxPower);
		minLa=1;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage,maxPower);
				if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa=i;
					minValLa=temp;
				}
			}
		}
		minValLit=Batteries.get(minLit).calculateCostNoCycles(TotWh,SystemVoltage,maxPower);
		minValLa=Batteries.get(minLa).calculateCostNoCycles(TotWh,SystemVoltage,maxPower);
		System.out.printf("%n%nFor Air Conditioners we suggest the following batteries:%n");
		if(minValLit<(minValLa*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))) {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Air Conditioners", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLit).getWh()))));
		}
		else {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLa), "Air Conditioners", (int)((SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLa).getWh())*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))));
		}
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		System.out.printf("The total cost of these batteries is %dE%n",(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		System.out.printf("With a daily usage of 5 hours during summer their estimated life time is %d years",UsedBatteries.get(UsedBatteries.size()-1).getCycles()/100);
		ACWh=ACWh+(int)((TotWh*30)/DoD);
	}


	private void WaterHeaterCalculator(){
		//int TotWh = (int)(NumberP*WaterHeater.getAverageConsumption()*1.3);
		int TotWh = 0; //(int)( NumberWM*WashingMachine.getAverageConsumption()*1.3);
		Iterator<Appliance> it = WaterHeaters.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TotWh=TotWh+tempa.getAverageConsumption();
		}
		if(WaterHeaters.size()!=0)TotWh=(int)((TotWh*DoD*NumberP*MaxPercentage)/WaterHeaters.size());
		//int TotW = (int)( (NumberWH*WaterHeater.getMaxPower()));
		int TotW=0;
		it = WaterHeaters.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TotW=TotW+tempa.getAverageConsumption();
		}
		double WorkTime = (TotWh/(DoD*MaxPercentage))/TotW;
		int i;
		double temp;
		double minValLit;
		int minLit;
		double minValLa;
		int minLa;
		double minValLit2;
		int minLit2;
		double minValLa2;
		int minLa2;
		double peakWh;
		int totalCost1stWH;
		int totalCost1stW;


		//WATT*HOUR FIRST
		minValLit=Batteries.get(0).calculateCost(TotWh,SystemVoltage);
		minLit=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage);
				if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit=i;
					minValLit=temp;
				}
			}
		}
		peakWh=((TotW/WaterHeaters.size())-Batteries.get(minLit).getDischargePower())*WorkTime*WaterHeaters.size()*MaxPercentage;
		minValLa=Batteries.get(1).calculateCost((int)peakWh,SystemVoltage);
		minLa=1;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost((int)peakWh,SystemVoltage);
				if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa=i;
					minValLa=temp;
				}
			}
		}
		totalCost1stWH=(int)(((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh-peakWh)/Batteries.get(minLit).getWh()))*Batteries.get(minLit).getPrice()+
				(Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles())*(SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil(peakWh/Batteries.get(minLa).getWh()))*Batteries.get(minLa).getPrice());

		//WATT FIRST
		minValLit2=Batteries.get(0).calculateCostWatt(TotW,SystemVoltage);
		minLit2=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCostWatt(TotW,SystemVoltage);
				if (temp<=minValLit2&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit2=i;
					minValLit2=temp;
				}
			}
		}
		peakWh=((TotW/WaterHeaters.size())-Batteries.get(minLit).getDischargePower())*WorkTime*WaterHeaters.size()*MaxPercentage;
		minValLa2=Batteries.get(1).calculateCost((int)peakWh,SystemVoltage);
		minLa2=1;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost((int)peakWh,SystemVoltage);
				if (temp<=minValLa2&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa2=i;
					minValLa2=temp;
				}
			}
		}
		totalCost1stW=(int)(((SystemVoltage/Batteries.get(minLit2).getVoltage())*Math.ceil((TotWh-peakWh)/Batteries.get(minLit2).getWh()))*Batteries.get(minLit2).getPrice()+
				(Math.floor(Batteries.get(minLit2).getCycles()/Batteries.get(minLa2).getCycles())*(SystemVoltage/Batteries.get(minLa2).getVoltage())*Math.ceil(peakWh/Batteries.get(minLa2).getWh()))*Batteries.get(minLa2).getPrice());

		System.out.printf("%n%nFor Water Heaters we suggest the following batteries:%n");

		//COMPARISON
		if (totalCost1stWH<totalCost1stW){

			UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Water Heaters", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh-peakWh)/Batteries.get(minLit).getWh()))));
			System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
			UsedBatteries.add(new UsedBattery(Batteries.get(minLa), "Water Heaters", (int)(Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles())*(SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil(peakWh/Batteries.get(minLa).getWh()))));

		}
		else{

			UsedBatteries.add(new UsedBattery(Batteries.get(minLit2), "Water Heaters", (int)((SystemVoltage/Batteries.get(minLit2).getVoltage())*Math.ceil((TotWh-peakWh)/Batteries.get(minLit2).getWh()))));
			System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
			UsedBatteries.add(new UsedBattery(Batteries.get(minLa2), "Water Heaters", (int)(Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa2).getCycles())*(SystemVoltage/Batteries.get(minLa2).getVoltage())*Math.ceil(peakWh/Batteries.get(minLa2).getWh()))));

		}
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		System.out.printf("The total cost of these batteries is %d E%n",(UsedBatteries.get(UsedBatteries.size()-2).getPrice()*UsedBatteries.get(UsedBatteries.size()-2).getQuantity())+(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		System.out.printf("With a daily usage their estimated life time is %d years",UsedBatteries.get(UsedBatteries.size()-2).getCycles()/365);
		AppWh=AppWh+(int)(((TotWh)*30)/DoD);
	}


	private void KitchenCalculator(){
		int TotWh=0;
		int MaxW=0;
		int TempMaxW=0;
		Iterator<Appliance> it = Dishwashers.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TempMaxW=TempMaxW+tempa.getMaxPower();
			TotWh=TotWh+tempa.getAverageConsumption();
			AppWh=AppWh+(int)(tempa.getAverageConsumption()*12*MaxPercentage);
		}
		if(TempMaxW>MaxW)MaxW=TempMaxW;
		TempMaxW=0;
		it = Ovens.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TempMaxW=TempMaxW+tempa.getMaxPower();
			TotWh=TotWh+tempa.getAverageConsumption();
			AppWh=AppWh+(int)(tempa.getAverageConsumption()*10*MaxPercentage);
		}
		if(TempMaxW>MaxW)MaxW=TempMaxW;
		TempMaxW=0;
		it = Microwaves.iterator();
		while(it.hasNext()){
			Appliance tempa = it.next();
			TempMaxW=TempMaxW+tempa.getMaxPower();
			TotWh=TotWh+(tempa.getAverageConsumption()*MinMW);
			AppWh=AppWh+(int)(tempa.getAverageConsumption()*20*MaxPercentage);
		}
		if(TempMaxW>MaxW)MaxW=TempMaxW;
		TotWh=(int)(TotWh*DoD*MaxPercentage);
		int i;
		double temp;
		double minValLit;
		int minLit;
		double minValLa;
		int minLa;

		minValLit=Batteries.get(0).calculateCost(TotWh,SystemVoltage, MaxW);
		minLit=0;
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==1){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage, MaxW);
				if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLit=i;
					minValLit=temp;
				}
			}
		}

		minValLa=Batteries.get(1).calculateCost(TotWh,SystemVoltage,MaxW);
		minLa=1;
		System.out.printf("%n%nFor Ovens, Microwaves and Dishwashers we suggest the following batteries:%n");
		for (i=1;i<Batteries.size();i++){
			if(Batteries.get(i).getType()==2){
				temp=Batteries.get(i).calculateCost(TotWh,SystemVoltage, MaxW);
				if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
					minLa=i;
					minValLa=temp;
				}
			}
		}
		minValLit=Batteries.get(minLit).calculateCostNoCycles(TotWh,SystemVoltage,MaxW);
		minValLa=Batteries.get(minLa).calculateCostNoCycles(TotWh,SystemVoltage,MaxW);
		if(minValLit<(minValLa*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))) {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Ovens DishWashers and Microwaves", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLit).getWh()))));
		}
		else {
			UsedBatteries.add(new UsedBattery(Batteries.get(minLa),"Ovens DishWashers and Microwaves", (int)((SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil((TotWh)/Batteries.get(minLa).getWh())*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))));
		}
		System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
		System.out.printf("The total cost of these batteries is %dE%n",(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		System.out.printf("With an average use of 135 cycles per year estimated life time is %d years",UsedBatteries.get(UsedBatteries.size()-1).getCycles()/135);
	}

	private void ExtraCalculator(){
		int deltaWh;
		if(NumberP==1)AppWh=(int)(AppWh*0.7);
		if(NumberP==2)AppWh=(int)(AppWh*0.85);
		if(NumberP>4)AppWh=(int)(AppWh*(1+(0.15*(NumberP-4))));
		if (BillSummer==1){
			deltaWh=(BillWh-AppWh-ACWh)/30;
		}
		else deltaWh=(BillWh-AppWh)/30;
		if (deltaWh>0){
			int i;
			double temp;
			double minValLit;
			int minLit;
			double minValLa;
			int minLa;
			
			deltaWh=(int)(deltaWh*DoD*MaxPercentage);
			minValLit=Batteries.get(0).calculateCost(deltaWh,SystemVoltage);
			minLit=0;
			for (i=1;i<Batteries.size();i++){
				if(Batteries.get(i).getType()==1){
					temp=Batteries.get(i).calculateCost(deltaWh,SystemVoltage);
					if (temp<=minValLit&&Batteries.get(i).getVoltage()<=SystemVoltage){
						minLit=i;
						minValLit=temp;
					}
				}
			}

			minValLa=Batteries.get(1).calculateCost(deltaWh,SystemVoltage);
			minLa=1;
			for (i=1;i<Batteries.size();i++){
				if(Batteries.get(i).getType()==2){
					temp=Batteries.get(i).calculateCost(deltaWh,SystemVoltage);
					if (temp<=minValLa&&Batteries.get(i).getVoltage()<=SystemVoltage){
						minLa=i;
						minValLa=temp;
					}
				}
			}
			minValLit=Batteries.get(minLit).calculateCostNoCycles(deltaWh,SystemVoltage);
			minValLa=Batteries.get(minLa).calculateCostNoCycles(deltaWh,SystemVoltage);
			System.out.printf("%n%nFor the remaining load we suggest the following batteries:%n");
			if(minValLit<(minValLa*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))) {
				UsedBatteries.add(new UsedBattery(Batteries.get(minLit), "Remaining Load", (int)((SystemVoltage/Batteries.get(minLit).getVoltage())*Math.ceil((deltaWh)/Batteries.get(minLit).getWh()))));
			}
			else {
				UsedBatteries.add(new UsedBattery(Batteries.get(minLa), "Remaining Load", (int)((SystemVoltage/Batteries.get(minLa).getVoltage())*Math.ceil((deltaWh)/Batteries.get(minLa).getWh())*Math.floor(Batteries.get(minLit).getCycles()/Batteries.get(minLa).getCycles()))));
			}
			System.out.printf("%s%n",UsedBatteries.get(UsedBatteries.size()-1).toString());
			System.out.printf("The total cost of these batteries is %dE%n",(UsedBatteries.get(UsedBatteries.size()-1).getPrice()*UsedBatteries.get(UsedBatteries.size()-1).getQuantity()));
		}
	}

	private void PrintBatteriesList(List<UsedBattery> list){
		int i;
		int Cost=0;
		String temp=null;
		UsedBattery tempBatt = null;
		PrintWriter writer = null;
		try {
			writer = new PrintWriter("Output/output.csv","UTF-8");
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		for(i=0;i<list.size();i++){
			Cost=Cost+(list.get(i).getPrice()*list.get(i).getQuantity());
			//		System.out.printf("%n%s%n", list.get(i).toString());
			//		if(list.get(i).getUsedBy().equals(temp))
			//		{
			//			System.out.printf("%n%s", list.get(i).toCSV());
			//		}
			//		else{
			//			System.out.printf("%n%nFor %s we suggest the following batteries:%n", list.get(i).getUsedBy());
			//			System.out.printf("%s", list.get(i).toCSV());
			//			temp=list.get(i).getUsedBy();
			//		}
			writer.println(list.get(i).toCSV());
			//			System.out.printf("%n%s", list.get(i).toCSV());
		}
		writer.close();
		System.out.printf("%nThe total cost of the system is: %d",Cost);

	}

}
